import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login/login.vue') // 路由懒加载  注释表示打包分类
  },
  {
    path: '/reg',
    component: () => import('../views/Reg/reg.vue') // 路由懒加载  注释表示打包分类
  },
  {
    path: '/',
    component: () => import('../views/Layout/layout.vue'),
    children: [
      { path: '', component: () => import('../views/Home/home.vue') },
      { path: 'artcategory', component: () => import('../views/Art/artcategory.vue') },
      { path: 'artlist', component: () => import('../views/Art/artlist.vue') },
      { path: 'userinfo', component: () => import('../views/User/userinfo.vue') },
      { path: 'changeavatar', component: () => import('../views/User/changeavatar.vue') },
      { path: 'resetpwd', component: () => import('../views/User/resetpwd.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
const whitelist = ['/login', '/regf']
router.beforeEach((to, from, next) => {
  console.log(whitelist.includes(to.path) === true)
  if (whitelist.includes(to.path) === true) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
